package com.video_study.service.serviceImpl;

import com.video_study.dao.CommentMapper;
import com.video_study.dao.UserApproveMapper;
import com.video_study.dto.CommentDto;
import com.video_study.pojo.Comment;
import com.video_study.pojo.UserApprove;
import com.video_study.result.ExceptionMsg;
import com.video_study.result.ResponseData;
import com.video_study.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ComentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserApproveMapper userApproveMapper;
    @Override
    public ResponseData getCommentsById(String id) {
        List<CommentDto> comments = commentMapper.selectCommentsById(id);
        return new ResponseData(comments);
    }

    @Override
    @Transactional
    public ResponseData sendComment(Comment comment) {
        try {
            String id = UUID.randomUUID().toString();
            comment.setId(id);
            comment.setCreateTime(new Date());
            Integer result = commentMapper.sendComment(comment);
            if(result>0){
                return new ResponseData(ExceptionMsg.SUCCESS);
            }else{
                return new ResponseData(ExceptionMsg.FAILD);
            }
        }catch(Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData("000407","未知异常");

        }
    }

    @Override
    public ResponseData getApproves(UserApprove userApprove) {
        UserApprove approve =  userApproveMapper.getApproves(userApprove);
        if( approve!=null ){
            return new ResponseData(ExceptionMsg.SUCCESS);
        }
        return new ResponseData(ExceptionMsg.FAILD);
    }
}
